package com.evs.system.department.controller;

import com.evs.system.department.service.IDepartmentService;
import com.evs.system.department.domain.Department;
import com.evs.system.department.query.DepartmentQuery;
import com.evs.utils.AjaxResult;
import com.evs.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/department")
@Api(value = "部门的API",description = "部门的crud")
public class DepartmentController {

    @Autowired
    public IDepartmentService departmentService;

    /**
     * 保存和修改公用的
     * @param department  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @ApiOperation(value = "部门的添加或者修改部门【如有id为修改操作】")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if( department.getId()!=null)
                departmentService.update(department);
            else
                departmentService.add(department);
            return AjaxResult.success().setMsg("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg("保存失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @ApiOperation(value = "根据id删除一条数据")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            departmentService.remove(id);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error().setMsg("删除失败！"+e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询一条数据")
    public Department queryById(@PathVariable("id")Long id)
    {
        return departmentService.queryById(id);
    }

    @PatchMapping("/del")
    @ApiOperation(value = "根据id批量删除")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.batchDelete(ids);
            return AjaxResult.success().setMsg("批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }

    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping("/list")
    @ApiOperation(value = "查询所有数据")
    public List<Department> queryAll(){
        return departmentService.queryAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/pageList")
    @ApiOperation(value = "分页所有查询")
    public AjaxResult pageList(@RequestBody DepartmentQuery query){
        try {
            List<Department> page = departmentService.queryPage(query);
            Long count = departmentService.queryCount(query);
            return AjaxResult.success().setData(page).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
            return AjaxResult.error().setMsg("获取分页数据失败");
    }
    @GetMapping("/tree")
    @ApiOperation(value = "返回部门树")
    public List<Department> getDepartmentTree(){
        return  departmentService.getDepartmentTree();
    }

    @GetMapping("/export")
    public void Export(HttpServletResponse response){
        try {
            //查询全部数据
            List<Department> departments = departmentService.queryAll();
            //使用工具类排版数据
            ExcelUtil.exportExcel(departments,"部门详细信息","部门数据",Department.class,"部门数据的详细信息.xls",response);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        List<Department> Departments = ExcelUtil.importExcel(file, 1, 1, Department.class);
        Departments.forEach(System.out::println);
    }
}
